/*
 * Created on 05.11.2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.nsu.konus.control.commands;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Dragon
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class StringCommand implements SpecialCommand{

	/**
	 * @param name
	 * @param parameters
	 */
	public StringCommand(String prefix) {
		this.prefix = prefix;
	}

	/* (non-Javadoc)
	 * @see com.nsu.konus.control.commands.SpecialCommand#getPrefix()
	 */
	public String getPrefix() {
		return prefix;
	}

	/* (non-Javadoc)
	 * @see com.nsu.konus.control.commands.SpecialCommand#getCommands(java.lang.String)
	 */
	/*
	public Command[] getCommands(String parameters) {
		byte[] bytes = parameters.getBytes();
		byte[] lowBytes = parameters.toLowerCase().getBytes();
		boolean up = false;
		int symbolCount = bytes.length;
		for(int i = 0; i < parameters.length(); i++) {
			if ((bytes[i] != lowBytes[i] ^ up) == true) {
				symbolCount++;
				up = !up;
			}
		}
		if (up) symbolCount++;
		Command[] commands = new Command[symbolCount];
		
		up = false;
		for(int i = 0, j = 0; i < bytes.length; i++, j++) {
			if ((bytes[i] != lowBytes[i] ^ up) == true) {
				char action = up ? Command.KEY_UP : Command.KEY_DOWN;
				commands[j++] = new Command(
						"", ControlChars.LSHIFT, action);
				up = !up;
			}
			commands[j] = new Command(
					"", ControlChars.getSymbol((char)lowBytes[i]), Command.KEY_DOWN_UP);
		}
		if (up) commands[symbolCount - 1] =  new Command(
				"", ControlChars.LSHIFT, Command.KEY_UP);
		
		return commands;
	}
	*/
	public Command[] getCommands(String parameters) {
		byte[] bytes = parameters.getBytes();
		List commandsList = new ArrayList();
		
		boolean up = false;
		for(int i = 0, j = 0; i < bytes.length; i++, j++) {
			ControlSymbol cs = ControlChars.getSymbol((char)bytes[i]);
			if (cs.isUseShift() != up) {
				char action = up ? Command.KEY_UP : Command.KEY_DOWN;
				commandsList.add(new Command(
						"", ControlChars.LSHIFT, action));
				up = !up;
			}
			commandsList.add(new Command(
					"", cs.getKey(), Command.KEY_DOWN_UP));
		}
		if (up) commandsList.add(new Command(
				"", ControlChars.LSHIFT, Command.KEY_UP));
		
		return (Command[])commandsList.toArray(new Command[commandsList.size()]);
	}
	
	private String prefix;
}
